import { defineStore } from 'pinia'
import { ref } from 'vue'

// 创建游客的store
export const useTouristStore = defineStore(
  'tourist',
  () => {
    const token = ref('')
    const id = ref(0)
    const name = ref('')
    const lastUpdated = ref(0)
    const setToken = (newToken) => {
      token.value = newToken
      lastUpdated.value = Date.now()
    }
    const removeToken = () => {
      token.value = ''
    }
    const setId = (newToken) => {
      id.value = newToken
    }
    const removeId = () => {
      id.value = 0
    }
    // const setLastUpdated = (newToken) => {
    //   lastUpdated.value = newToken
    // }
    // const removeLastUpdated = () => {
    //   lastUpdated.value = 0
    // }
    const setName = (newToken) => {
      name.value = newToken
    }
    const removeName = () => {
      name.value = ''
    }
    const isTokenExpired = () => {
      const EXPIRY_DAYS = 7
      const expiryTime = lastUpdated.value + EXPIRY_DAYS * 24 * 60 * 60 * 1000
      return Date.now() > expiryTime
    }
    return {
      token,
      setToken,
      removeToken,
      isTokenExpired,
      id,
      setId,
      removeId,
      name,
      setName,
      removeName,
      lastUpdated,
    }
  },
  {
    persist: true,
  },
)
